package com.itheima.device.controller;



import com.github.pagehelper.Page;
import com.itheima.device.pojo.Device;
import com.itheima.device.pojo.DeviceInfo;
import com.itheima.device.service.DeviceService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin//开启跨越请求操作
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 查询全部设备
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Device> deviceList = deviceService.findAll();
            return  new Result(true, StatusCode.OK,"查询设备成功",deviceList);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,StatusCode.ERROR,"查询设备失败");
        }

    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public  Result  findPage(@PathVariable int page,@PathVariable int size){
        try {
            Page<Device> pageList = deviceService.findPage(page, size);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,"查询分页成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.OK,"分页查询失败");
        }

    }

    /**
     * 根据设备名称id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public  Result findByDevice  (@PathVariable  Integer id) {
        Device device1 = null;
        try {
            device1 = deviceService.findByDevice(id);
            return new Result(true, StatusCode.OK, "设备查询成功", device1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "设备查询失败");

        }

    }

    //1.查询设备与指标的关联关系


    //2.添加设备时没有添加设备说明书



    /**
     * 新增设备
     * @param device
     * @return
     */
    @PostMapping("/add")
    public  Result add (@RequestBody Device device){
        try {
            deviceService.add(device);
            return new Result(true,StatusCode.OK,"新增设备成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"新增设备失败");
        }
    }

    /*
     * 修改设备信息
     * @param device
     * @return
     */
    @PutMapping("/update")
    public Result updateDevice(@RequestBody Device device  ){
        try {
            deviceService.updateDevice(device);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }

    }

    /**
     * 根据id删除设备
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            deviceService.delete(id);
            return new Result(true,StatusCode.OK,"删除设备id成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false ,StatusCode.ERROR,"删除设备id失败");
        }

    }

    /**
     * 根据设备id查询设备和设备使用说明书
     * @param did
     * @return
     */
    @GetMapping("/findDeviceInfoByDid/{did}")
    public  Result findDeviceInfoByDid(@PathVariable Integer did){
        DeviceInfo deviceInfo = null;
        try {
            List<DeviceInfo> deviceInfoByDid = deviceService.findDeviceInfoByDid(did);
            return new Result(true,StatusCode.OK,"查询成功",deviceInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }

    /**
     * 根据设备id删除设备和设备使用说明书
     * @param did
     * @return
     */
    @DeleteMapping("/deleteDeviceInfoByDid/{did}")
    public  Result deleteDeviceInfoByDid(@PathVariable Integer did){
        try {
             deviceService.deleteDeviceInfoByDid(did);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, StatusCode.ERROR,"删除失败");
        }

    }


    /**
     * 查询 设备 设备使用说明 设备指标
     * @param id
     * @return
     */
    @GetMapping("/findAllDevice/{id}")
    public  Result findAllDevice(@PathVariable Integer id){
        try {
            List<Device> deviceIndexList = deviceService.findDeviceById(id);
            return new Result(true,StatusCode.OK,"查询成功",deviceIndexList);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,StatusCode.ERROR,"查询失败");
        }

    }





}

